/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.curso.sisweb.bean;


import com.curso.sisweb.modelo.Cliente;
import com.curso.sisweb.modelo.Pago;
import com.curso.sisweb.modelo.PagoJpaController;
import java.io.*;
import java.sql.Date;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.*;

import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author charlyx
 */

 
@ManagedBean(name="fileUploadController")
@SessionScoped
public class FileUploadController implements Serializable {
   @Resource
   private UserTransaction utx = null;
   @PersistenceUnit(unitName = "com.curso.web_sisweb_war_1.0-SNAPSHOTPU")
   private EntityManagerFactory emf = null;
  // private String destination= "/home/charlyx/tmp/";   //"D:\\tmp\\";
   private String line;
   private StringTokenizer st;
   private Pago pago = null;
   //private PagoController pgc = null;
   private PagoJpaController pjc = null;
   //private String m;

    public void upload(FileUploadEvent event) {  
        try {
            FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
            FacesContext.getCurrentInstance().addMessage(null, msg);
            pjc = new PagoJpaController(utx, emf);

                InputStream data = event.getFile().getInputstream();
                DataInputStream in = new DataInputStream(data);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
               
                // Leemos linea a linea el archivo.
                while ((line = br.readLine()) != null) 
                { 
                    
                    pago =  new Pago();
                    st = new StringTokenizer(line, ";"); 
                    System.out.println("ARCHIVO *** :" + line);
                    //System.out.println("Tokn *** :" + st.nextToken());
                    //System.out.println("Tokn *** :" + st.nextToken());
                    //System.out.println("Tokn *** :" + st.nextToken());
                    //System.out.println("Tokn *** :" + st.nextToken()); 
                      pago.setIdPago(Integer.parseInt(st.nextToken()));
                      pago.setIdCliente(new Cliente(Integer.parseInt(st.nextToken())));
                      pago.setMonto(Integer.parseInt(st.nextToken()));
                      pago.setFecha(Date.valueOf(st.nextToken()));
                      pjc.create(pago);
                       //System.out.println("Tokns *** :" + m);
                    
                } 
                br.close();
        } catch (IOException ex) {
            Logger.getLogger(FileUploadController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e){
            e.printStackTrace();
        }
        
        

    }  

}

